package com.ssy.jt808.gateway.client.exception;

import com.ssy.gaea.common.exception.BaseException;
import org.springframework.http.HttpStatus;

import java.util.Collections;

/**
 * @author zeno
 */
public class JtGatewayRequestException extends BaseException{

  /** http 状态码 */
  private final HttpStatus status;

  /** 异常状态码 */
  private final String code;


  public JtGatewayRequestException( String msg ){
    this(HttpStatus.INTERNAL_SERVER_ERROR, msg);
  }

  public JtGatewayRequestException( String code, String msg ){
    this(HttpStatus.INTERNAL_SERVER_ERROR, code, msg);
  }

  public JtGatewayRequestException( String code, String msgPattern, Object... msgArgs ){
    this(HttpStatus.INTERNAL_SERVER_ERROR, code, msgPattern, msgArgs);
  }

  public JtGatewayRequestException( String msgPattern, Object... msgArgs ){
    this(HttpStatus.INTERNAL_SERVER_ERROR, msgPattern, msgArgs);
  }

  /***
   *
   * @param status      http 状态码
   * @param msg         异常消息
   */
  public JtGatewayRequestException( HttpStatus status, String msg ){
    this(status.name(), status, msg);
  }

  /***
   *
   * @param status      http 状态码
   * @param msgPattern  java.text.MessageFormat 消息模板
   * @param msgArgs     java.text.MessageFormat 消息模板参数
   */
  public JtGatewayRequestException( HttpStatus status, String msgPattern, Object... msgArgs ){
    this(status.name(), status, msgPattern, msgArgs);
  }

  /***
   *
   * @param status      http 状态码
   * @param code        异常状态码
   */
  public JtGatewayRequestException( String code, HttpStatus status, String msg ){
    this(code, status, msg, Collections.emptyList());
  }

  /***
   *
   * @param status      http 状态码
   * @param code        异常状态码
   * @param msgPattern  java.text.MessageFormat 消息模板
   * @param msgArgs     java.text.MessageFormat 消息模板参数
   */
  public JtGatewayRequestException( String code, HttpStatus status, String msgPattern, Object... msgArgs ){
    this(code, status, null, msgPattern, msgArgs);
  }

  /***
   *
   * @param throwable   异常栈
   */
  public JtGatewayRequestException( Throwable throwable ){
    this(HttpStatus.INTERNAL_SERVER_ERROR.name(),
         HttpStatus.INTERNAL_SERVER_ERROR,
         throwable,
         throwable.getMessage(),
         Collections.emptyList());
  }

  /***
   *
   * @param code        异常状态码
   * @param throwable   异常栈
   */
  public JtGatewayRequestException( String code, Throwable throwable ){
    this(code, throwable, throwable.getMessage(), Collections.emptyList());
  }

  /***
   *
   * @param code        异常状态码
   * @param throwable   异常栈
   * @param msgPattern  java.text.MessageFormat 消息模板
   * @param msgArgs     java.text.MessageFormat 消息模板参数
   */
  public JtGatewayRequestException( String code, Throwable throwable, String msgPattern, Object... msgArgs ){
    this(code, HttpStatus.INTERNAL_SERVER_ERROR, throwable, msgPattern, msgArgs);
  }

  /***
   *
   * @param code        异常状态码
   * @param status      http 状态码
   * @param throwable   异常栈
   * @param msgPattern  java.text.MessageFormat 消息模板
   * @param msgArgs     java.text.MessageFormat 消息模板参数
   */
  public JtGatewayRequestException( String code,
                                    HttpStatus status,
                                    Throwable throwable,
                                    String msgPattern,
                                    Object... msgArgs ){
    super(throwable, msgPattern, msgArgs);
    this.code = code;
    this.status = status;
  }

  @Override
  public String module(){
    return "Jt808-Gateway-Client";
  }

  public HttpStatus status(){
    return status;
  }

  public String code(){
    return code;
  }
}
